package com.seari.service.impl;


import com.seari.bean.EtMaintainPlan;
import com.seari.bean.EtMaintainRecords;
import com.seari.mapper.EtMaintainPlanMapper;
import com.seari.pojo.EtmaintainPlanParam;
import com.seari.service.EtMaintainPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import java.util.List;


@Service("etMaintainPlanService")
public class EtMaintainPlanServiceImpl implements EtMaintainPlanService {

    @Autowired
    private EtMaintainPlanMapper etMaintainPlanMapper;

    @Override
    public List<EtMaintainPlan> getEtmaintainPlan(EtmaintainPlanParam param) {
        /* 先转成毫秒并求差 */
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date d1;
        Date d2;
        long diff;
        long days;
        String day;
        List<EtMaintainPlan> lists =  etMaintainPlanMapper.getEtmaintainPlan(param);
        for ( EtMaintainPlan list : lists) {
            String maintainthisTime = list.getMaintainthisTime();
            String maintainnextTime = list.getMaintainnextTime();
            if (null != maintainthisTime && null !=maintainnextTime){
                try {
                    d1 = sdf.parse(maintainthisTime);
                    d2 = sdf.parse(maintainnextTime);
                    diff = d2.getTime() - d1.getTime();
                    days = diff / (1000 * 60 * 60 * 24);
                    day = String.valueOf(days);
                    list.setIntervalDay(day+"天");
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }else {
                list.setIntervalDay("维修时间异常");
            }
        }
       return lists;
    }

    @Override
    public List<Integer> getTaskNum(EtmaintainPlanParam param) {

        return etMaintainPlanMapper.getTaksNum(param);
    }

    @Override
    public List<EtMaintainPlan> getEtmaintainPlans(EtmaintainPlanParam param) {

        /* 先转成毫秒并求差 */
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date d1;
        Date d2;
        long diff;
        long days;
        String day;
        List<EtMaintainPlan> lists =  etMaintainPlanMapper.getEtmaintainPlans(param);
        for ( EtMaintainPlan list : lists) {
            String maintainthisTime = list.getMaintainthisTime();
            String maintainnextTime = list.getMaintainnextTime();
            if (null != maintainthisTime && null !=maintainnextTime){
                try {
                    d1 = sdf.parse(maintainthisTime);
                    d2 = sdf.parse(maintainnextTime);
                    diff = d2.getTime() - d1.getTime();
                    days = diff / (1000 * 60 * 60 * 24);
                    day = String.valueOf(days);
                    list.setIntervalDay(day+"天");
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }else {
                list.setIntervalDay("维修时间异常");
            }

        }
        return lists;
    }

    @Override
    public int addMaintain(EtMaintainPlan et) {

        return etMaintainPlanMapper.addMaintain(et);
    }

    @Override
    public int updateMaintain(EtMaintainPlan et) {

        return etMaintainPlanMapper.updateMaintain(et);
    }

    @Override
    public int deleteMaintain(String taskNum) {
        return etMaintainPlanMapper.deleteMaintain(taskNum);
    }

    @Override
    public List<EtMaintainRecords> getRecord(String doMain, String systemName, String eventType) {
        return etMaintainPlanMapper.getRecord(doMain,systemName,eventType);
    }

    @Override
    public int addRecord(EtMaintainRecords etm) {
        return etMaintainPlanMapper.addRecord(etm);
    }

    @Override
    public List<EtMaintainRecords> getRecords(EtmaintainPlanParam param) {
        return etMaintainPlanMapper.getRecords(param);
    }

    @Override
    public int updateRecords(EtMaintainRecords etm) {
        return etMaintainPlanMapper.updateRecords(etm);
    }

    @Override
    public int deleteRecord(String taskNum) {
        return etMaintainPlanMapper.deleteRecord(taskNum);
    }

    @Override
    public int deleteRecords(String[] taskNums) {
        return etMaintainPlanMapper.deleteRecords(taskNums);
    }


}
